<template>
  <div id="entBalances" class="card col-12 px-0 mt-5">
    <nav aria-label="breadcrumb" role="navigation">
      <ol class="breadcrumb pb-0">
        <li class="breadcrumb-item ms-1" aria-current="page">客户</li>

        <li class="breadcrumb-item active">授信</li>
      </ol>
    </nav>
    <div class="card-body">
      <form>
        <div class="row mb-2">
          <input
            type="textfield"
            class="form-control"
            placeholder="公司名称"
            name="sc.name"
            v-model.trim="name"
            size="15"
          />
        </div>
        <div class="row mb-2">
          <select class="form-select" v-model.number="sc.pageSize">
            <option value="5">5</option>
            <option value="10">10</option>
            <option value="20">20</option>
            <option value="50">50</option>
          </select>
        </div>
        <div class="row mb-2">
          <button type="button" class="btn btn-primary mb-2" @click="search()">
            查找
          </button>
          <button
            type="button"
            class="btn btn-secondary"
            title="清空查找条件"
            @click="reset()"
          >
            重置
          </button>
        </div>
      </form>
    </div>
    <table class="table table-striped table-hover table-sm">
      <thead>
        <tr>
          <th>被授权方</th>
          <th class="text-end">现金账户</th>
          <th class="text-end">信用额度</th>
          <th class="text-end">信用余额</th>
          <th>单位:元</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(info, index) in dataList">
          <td>
            {{ info.name }}
            <br />
            <span class="text-danger small">{{ info.customerCode }}</span>
          </td>
          <td class="text-end">
            
            <a
              href="javascript:void(0)"
              @click.stop="editCashBalance(index)"
              >{{ info.cashBalance / 100 }}</a
            >
          </td>
          <td class="text-end">
            
            <a
              href="javascript:void(0)"
              @click="editCreditLimit(index)"
              >{{ info.creditLimit / 100 }}</a
            >
          </td>
          <td class="text-end">
            {{ info.creditBalance / 100 }}
          </td>
          <td>
            <router-link :to="`/customer/` + info.enterpriseId + `/trans`"
              >交易记录</router-link
            >
          </td>
        </tr>
      </tbody>
    </table>
    <nav id="pagination-box" class="float-end">
      <my-pagination
        name="pagination"
        :row-count="sc.rowCount"
        :page-total="sc.pageTotal"
        :page-no="sc.pageNo"
        @next-page="nextPage"
        @prev-page="prevPage"
        @direct-page="directPage"
      ></my-pagination>
    </nav>

    <my-modal-customer-balance ref="customerBalanceModal" :dataList="dataList">
    </my-modal-customer-balance>
    <my-modal-customer-cash-balance
      ref="customerCashBalanceModal"
      :dataList="dataList"
    >
    </my-modal-customer-cash-balance>
  </div>
</template>

<script>
import {
  searchCustomerBalances,
  saveEntCreditLimit,
  saveEntCashBalance,
} from "@/api/user.js";
import $ from "jquery";
import MyPagination from "@/components/my-pagination.vue";
import MyModalCustomerBalance from "@/components/modal/my-modal-customer-balance.vue";
import MyModalCustomerCashBalance from "@/components/modal/my-modal-customer-balance-cash.vue";

export default {
  components: {
    MyPagination,
    MyModalCustomerBalance,
    MyModalCustomerCashBalance,
  },
  data() {
    return {
      authorizee: 0,
      creditLimit: 0,
      opMode: 0,
      amount: 0,
      remark: "",

      dataList: [],
      name: "",
      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 10,
        pageTotal: 0,
      },
    };
  },
  mounted: function () {
    this.search();
  },
  methods: {
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    search: function () {
      const params = {
        "sc.name": this.name,
        "sc.pageNo": this.sc.pageNo,
        "sc.pageSize": this.sc.pageSize,
      };

      searchCustomerBalances(params, (jsonResult) => {
        this.dataList = jsonResult.dataList;
        this.sc = jsonResult.page;
      });
    },
    reset: function () {
      this.name = "";
    },
    saveCreditLimit: function (v) {
      const params = {
        authorizee: v.authorizee,
        creditLimit: v.creditLimit,
      };

      saveEntCreditLimit(params, (jsonResult) => {
        if (jsonResult.status === "OK") {
          this.showErrMsg("操作成功");
          this.search();
        } else {
          this.showErrMsg("操作失败：" + jsonResult.errmsg);
        }
      });
    },
    editCreditLimit: function (index) {
      this.authorizee = this.dataList[index].enterpriseId;
      this.creditLimit = this.dataList[index].creditLimit / 100;

      this.$refs.customerBalanceModal
        .modal(this.authorizee, this.creditLimit)
        .then((v) => {
          this.saveCreditLimit(v);
        })
        .catch((ex) => {});
    },
    editCashBalance: function (index) {
      this.authorizee = this.dataList[index].enterpriseId;

      this.$refs.customerCashBalanceModal
        .modal(this.authorizee)
        .then((v) => {
          this.updateCashBalance(v);
        })
        .catch((ex) => {});
    },
    updateCashBalance: function (v) {
      const params = {
        id: v.authorizee,
        opMode: v.opMode,
        modifyAmount: v.amount,
        remark: v.remark,
      };

      saveEntCashBalance(params, (jsonResult) => {
        if (jsonResult.status === "OK") {
          this.showErrMsg("操作成功");
          this.search();
        } else {
          this.showErrMsg(jsonResult.errmsg);
        }
      });
    },
    getAuthorizeeName: function (enterpriseId) {
      for (let i = 0; i < this.customers.length; i++) {
        if (this.customers[i].enterpriseId === enterpriseId) {
          return this.customers[i].name;
        }
      }
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
  },
};
</script>